<template>
	<view class="container">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<icon type="search" :size='13' color='#333'></icon>
				<input confirm-type="search" :placeholder="'请输入想搜索的菜品名称:'+placeholderValue" :focus="true" auto-focus placeholder-class="tui-input-plholder"
				 class="tui-input" v-model.trim="key" />
				<icon type="clear" :size='13' color='#bcbcbc' @tap="cleanKey" v-show="key"></icon>
			</view>
			<view class="tui-cancle" @tap="back">取消</view>
		</view>

		<block v-if="!isSearch">
			<view class="tui-search-history" v-if="history.length>0">
				<view class="tui-history-header">
					<view class="tui-search-title">搜索历史</view>
					<tui-icon name="delete" :size='14' color='#333' @tap="openActionSheet" class="tui-icon-delete"></tui-icon>
				</view>
				<view class="tui-history-content">
					<block v-for="(item,index) in history" :key="index">
						<tui-tag type="gray" shape="circle">{{item}}</tui-tag>
					</block>
				</view>
			</view>
		</block>
		<block v-else>
			<view class="body-ul">
				<template v-if="classifyListData.length==0">
					<view class='noCard'>
						暂无该关键词菜谱
					</view>
				</template>
				<template v-else>
					<classify-list :classifyListData="classifyListData" :hideTitle="true"></classify-list>
					<loadmore :visible="loadding" :index="3" type="red"></loadmore>
					<nomore :visible="pullUpOn"></nomore>
				</template>
			</view>
		</block>
		<tui-actionsheet :show="showActionSheet" :tips="tips" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>
	</view>
</template>

<script>
	import tuiIcon from "@/components/icon";
	import tuiTag from "@/components/tag";
	import tuiActionsheet from "@/components/actionsheet";
	import classifyList from '../../components/classifyList.vue'
	import {
		debounce
	} from '../../util/common.js'
	export default {
		components: {
			tuiIcon,
			tuiTag,
			tuiActionsheet,
			classifyList
		},
		data() {
			return {
				history: [],
				classifyListData: [],
				key: "",
				showActionSheet: false,
				tips: "确认清空搜索历史吗？",
				isSearch: false,
				page: 1,
				limit: 10,
				loadding: false,
				pullUpOn: false,
				placeholderValue: ''
			}
		},
		methods: {
			back: function() {
				uni.navigateBack();
			},
			cleanKey: function() {
				this.key = ''
			},
			closeActionSheet: function() {
				this.showActionSheet = false
			},
			openActionSheet: function() {
				this.showActionSheet = true
			},
			itemClick: function(e) {
				let index = e.index;
				if (index == 0) {
					this.showActionSheet = false;
					this.history = []
				}
			},
			getMenuByName(callback) {
				return this.$api.getMenuByName({
					menuName: this.key,
					page: this.page,
					limit: this.limit
				})
			}
		},
		onReachBottom() {
			if (this.pullUpOn) return;
			this.loadding = true;
			this.page = this.page + 1;
			this.getMenuByName().then((res) => {
				if (res.code) {
					if (res.data.length < this.limit) {
						this.pullUpOn = true
					}
					this.classifyListData = this.classifyListData.concat(res.data);
					this.loadding = false
				} else {
					this.loadding = false;
					this.pullUpOn = true
				}
			})
		},
		watch: {
			key: debounce(function() {
				if (!String(this.key).length) {
					this.isSearch = false;
				} else {
					this.isSearch = true;
					this.history.push(this.key);
					var filtered = this.history.filter((element, index, array) => index >= array.length - 10);
					this.history = filtered;
					this.page = 1;
					this.loadding = false
					this.pullUpOn = false
					this.getMenuByName().then((res) => {
						if (res.code) {
							this.classifyListData = res.data;
						} else {
							this.classifyListData = []
						}
					})

				}
			}, 300)
		}
	}
</script>

<style>
	page {
		color: #333;
		background: #fff;
	}

	.container {
		padding: 0 30upx 30upx 30upx;
		box-sizing: border-box;
	}

	.tui-searchbox {
		padding: 30upx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-search-input {
		width: 100%;
		height: 66upx;
		border-radius: 35upx;
		padding: 0 30upx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16upx;
		font-size: 28upx;
	}

	.tui-input-plholder {
		font-size: 28upx;
		color: #b2b2b2;
	}

	.tui-cancle {
		color: #888;
		font-size: 28upx;
		padding-left: 30upx;
		flex-shrink: 0;
	}

	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30upx 0;
	}

	.tui-icon-delete {
		padding: 10upx;
	}

	.tui-search-title {
		font-size: 28upx;
		font-weight: bold;
	}

	.tui-hot-header {
		padding: 30upx 0;
	}

	.tui-tag-class {
		display: inline-block;
		margin-bottom: 20upx;
		margin-right: 20upx;
		font-size: 26upx !important;
	}

	.noCard {
		height: 200upx;
		width: 100%;
		margin: auto;
		background-color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #999999;
		box-shadow: 0 0 10upx 0 rgba(0, 0, 0, 0.10);
	}
</style>
